package com.dems.dao;

import com.dems.domain.Mp;
import com.dems.domain.Student;
import com.dems.util.JDBCUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class MpDao {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    
  //根据工号查询维修人员
  	public Mp selectOne(String mpNum) {
  		try {
              conn = JDBCUtils.getConnection();
              String sql = "select * from maintenance_personnel where mp_num=?";
              ps = conn.prepareStatement(sql);
              ps.setString(1,mpNum);
              rs = ps.executeQuery();
              if(rs.next()){
            	  Mp mp = new Mp();
            	  mp.setMpId(rs.getInt("mp_id"));
            	  mp.setMpNum(rs.getString("mp_num"));
            	  mp.setMpName(rs.getString("mp_name"));
            	  mp.setMpPassword(rs.getString("mp_password"));
            	  mp.setBuildingId(rs.getString("building_id"));
            	  mp.setMpType(rs.getString("mp_type"));
            	  mp.setMpPhone(rs.getString("mp_phone"));
                  return mp;
              }else{
                 return null;
              }
          }catch (Exception e){
              e.printStackTrace();
              return null;
          }finally {
              JDBCUtils.close(conn,ps,rs);
          }
  	}
    
    //根据宿舍楼号查询维修人员
  	public List<Mp> selectMpsByBuildingId(String buildingId) {
      	try {
              conn = JDBCUtils.getConnection();
              String sql = "select * from maintenance_personnel where building_id=?";
              ps = conn.prepareStatement(sql);
              ps.setString(1,buildingId);
              rs = ps.executeQuery();
              List<Mp> mpList = new ArrayList<>();
              while (rs.next()) {
              	  Mp mp = new Mp();
                  mp.setMpId(rs.getInt("mp_id"));
                  mp.setMpNum(rs.getString("mp_num"));
                  mp.setMpName(rs.getString("mp_name"));
                  mp.setMpPassword(rs.getString("mp_password"));
                  mp.setBuildingId(rs.getString("building_id"));
                  mp.setMpType(rs.getString("mp_type"));
                  mp.setMpPhone(rs.getString("mp_phone"));
                  mpList.add(mp);
              }
              return mpList;
          }catch (Exception e){
              e.printStackTrace();
              return null;
          }finally {
              JDBCUtils.close(conn,ps,rs);
          }
      }
  	
  	//修改学生信息
  	public boolean update(Mp mp) {
  		int i = 0;
  		try {
  			conn = JDBCUtils.getConnection();
  			ps = conn.prepareStatement("update maintenance_personnel set mp_name=?,mp_password=?,mp_phone=? where mp_num=?");
  			ps.setString(1, mp.getMpName());
  			ps.setString(2, mp.getMpPassword());
  			ps.setString(3, mp.getMpPhone());
  			ps.setString(4, mp.getMpNum());
  			i = ps.executeUpdate();
  		} catch (Exception e) {
  			e.printStackTrace();
  			return i>0;
  		}finally{
  			JDBCUtils.close(conn, ps, null);
  		}
  		return i>0;
  	}
    
    //登录
    public Mp login(String accountId, String password) {
        try {
            conn = JDBCUtils.getConnection();
            String sql = "select * from maintenance_personnel where mp_num=? and mp_password=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, accountId);
            ps.setString(2, password);
            rs = ps.executeQuery();
            if (rs.next()) {
                Mp mp = new Mp();
                mp.setMpId(rs.getInt("mp_id"));
                mp.setMpNum(rs.getString("mp_num"));
                mp.setMpName(rs.getString("mp_name"));
                mp.setMpPassword(rs.getString("mp_password"));
                mp.setBuildingId(rs.getString("building_id"));
                mp.setMpType(rs.getString("mp_type"));
                mp.setMpPhone(rs.getString("mp_phone"));
                return mp;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            JDBCUtils.close(conn, ps, rs);
        }
    }

    //注册
    public void regist(Mp mp) {
        try {
            conn = JDBCUtils.getConnection();
            ps = conn.prepareStatement("insert into maintenance_personnel values(null,?,?,?,?,?,?)");
            ps.setString(1, mp.getMpNum());
            ps.setString(2, "");
            ps.setString(3, mp.getMpPassword());
            ps.setString(4, mp.getBuildingId());
            ps.setString(5, mp.getMpType());
            ps.setString(6, "");
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            JDBCUtils.close(conn, ps, null);
        }
    }
    
    //忘记密码
    public boolean forget(String accountId, String phone, String password) {
        int i = 0;
        try {
            conn = JDBCUtils.getConnection();
            String sql = "update maintenance_personnel set mp_password=? where mp_num=? and mp_phone=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, password);
            ps.setString(2, accountId);
            ps.setString(3, phone);
            i = ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
            return i > 0;
        } finally {
            JDBCUtils.close(conn, ps, rs);
        }
        return i > 0;
    }
}
